<!--
 * @Descripttion:移动设备管理
-->
<template>
  <div>
    <div class="content-box">
      <div class="left-box">
        <div class="title-box">移动设备管理</div>
        <div class="table-list-box">
          <el-table v-loading="loading" element-loading-text="加载中..." :data="tableData" :height="601">
            <el-table-column align="center" width="55">
              <template #default="scope">
                <el-radio class="radio" v-model="templateSelection" :label="scope.row.mobile_id">&nbsp;</el-radio>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="mobile_id" label="ID" />
            <el-table-column align="center" prop="type" label="设备类型">
              <template #default="scope">{{ scope.row.type === 1 ? '平板' : '手机' }}</template>
            </el-table-column>
            <el-table-column align="center" prop="device_name" label="设备号" />
            <el-table-column align="center" prop="mobile_uuid" label="设备号" />
            <el-table-column align="center" prop="last_login_time" label="登录时间" />
            <el-table-column align="center" prop="status" label="状态">
              <template #default="scope">{{ scope.row.status === '1' ? '正常' : '禁用' }}</template>
            </el-table-column>
          </el-table>
        </div>
        <div class="desc-btn-box">
          <div class="table-total" v-show="total > 0">
            <span>共{{ total }}条</span>
            <div>
              <Pagination v-show="total > 0" :total="total" v-model:page="searchParam.page" v-model:limit="searchParam.limit" @pagination="getMobileDeviceList" :pageSizes="[15, 30, 45, 100]" />
            </div>
          </div>
          <div class="btn-box">
            <div class="update-status" @click="handleDelete">删除</div>
            <div class="update-status" @click="handleDisable()">{{ selectMobile?.status === '0' ? '启用' : '禁用' }}</div>
            <div class="update-status" @click="handleAction(4)">返回</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getMobileDeviceList as apiGetMobileDeviceList, deleteMobileDevice, disableMobileDevice } from '@/api/mobie'
import ItMessageService from '@/components/messageTips/index'
import Pagination from '@/components/Pagination/Index.vue'

const router = useRouter()
const tableData = ref([])
const loading = ref(false)
const searchParam = reactive({ page: 1, limit: 15 })
const total = ref(0)
const templateSelection = ref('')
const isShowAddCompunter = ref(false)

const selectMobile = computed(() => tableData.value.find(res => res.mobile_id === templateSelection.value))

function getMobileDeviceList() {
  loading.value = true
  apiGetMobileDeviceList(searchParam).then(res => {
    tableData.value = res.data.list.data
    total.value = res.data.list.total
    loading.value = false
  }).finally(() => {
    loading.value = false
  })
}

function handleDelete() {
  if (!templateSelection.value) { ElMessage.warning('请选择要删除的设备'); return }
  const mobileId = templateSelection.value
  ItMessageService.message({ onCancelText: '取消', onSumbitText: '确定', message: { title: '提示', content: '您确认要删除该设备吗？' } })
    .then(() => {
      deleteMobileDevice({ mobile_id: mobileId }).then(res => {
        if (res.status === 200) { getMobileDeviceList(); ElMessage.success('删除成功') }
      })
    })
    .catch(() => {})
}

function handleDisable() {
  if (!templateSelection.value) { ElMessage.warning('请选择要禁用的设备'); return }
  const mobileStatus = selectMobile.value?.status === '0' ? '启用' : '禁用'
  const payload = { mobile_id: templateSelection.value, status: mobileStatus === '启用' ? '1' : '0' }
  ItMessageService.message({ onCancelText: '取消', onSumbitText: '确定', message: { title: '提示', content: `您确认要${mobileStatus}该设备吗？` } })
    .then(() => {
      disableMobileDevice(payload).then(res => { if (res.status === 200) { getMobileDeviceList(); ElMessage.success(`${mobileStatus}成功`) } })
    })
    .catch(() => {})
}

function handleAction() { router.go(-1) }

getMobileDeviceList()
</script>
  
<style lang="scss" scoped>
  :deep(.el-table) {
    border-radius: 12px;
  }
  :deep(.el-table__body-wrapper) {
    height: 601px;
    overflow-y: auto;
  }
  //滚动条整体的大小
  :deep(.el-table__body-wrapper)::-webkit-scrollbar {
    width: 6px !important;
    height: 0 !important;
  }
  //滚动条里滑动的滑块
  :deep(.el-table__body-wrapper)::-webkit-scrollbar-thumb {
    height: 200px !important;
    background: #d2d2d2 !important;
    border-radius: 3px !important;
  }
  //滚动条底层样式
  :deep(.el-table__body-wrapper)::-webkit-scrollbar-track {
    display: none !important;
  }
  :deep(.el-table th.el-table__cell) {
    background: #a85b62;
    color: #fff !important;
  }
  :deep(.el-input__inner) {
    border-radius: 12px;
  }
.content-box {
  display: flex;
  align-items: flex-start;
  box-sizing: border-box;

  .left-box {
    width: 1600px;
    height: 986px;
    background: #ffffff;
    border-radius: 20px;
    box-shadow: 3px 0px 24px 0px rgba(0, 0, 0, 0.16);
    box-sizing: border-box;
    .title-box {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 88px;
      background: #f4f4f4;
      border-radius: 20px 20px 0px 0px;
      font-size: 20px;
      font-family: PingFang SC, PingFang SC-Bold;
      font-weight: 700;
      text-align: center;
      color: #333333;
    }

    .table-list-box {
      padding: 20px 30px 0 30px;
      height: 681px;
      box-sizing: border-box;
    }

    .desc-btn-box {
      display: flex;
      flex-direction: column;
      // align-items: center;
      // justify-content: space-between;
      margin-top: 80px;
      padding: 0px 50px;
      gap: 20px;
      box-sizing: border-box;
      .desc {
        font-size: 14px;
        font-family: PingFang SC, PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
      }
      .table-total {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      .btn-box {
        display: flex;
        align-items: center;
        // justify-content: space-around;
        justify-content: flex-end;
        div:hover {
          color: #7c0a03;
          border-color: #7c0a03;
        }
        .add-printer {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 174px;
          height: 52px;
          background: #ffffff;
          border: 1px solid #333333;
          border-radius: 12px;
          font-size: 18px;
          font-family: PingFang SC, PingFang SC-Regular;
          font-weight: 400;
          color: #333333;
          margin-right: 24px;
          cursor: pointer;
        }
        .update-status {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 120px;
          height: 52px;
          background: #ffffff;
          border: 1px solid #333333;
          border-radius: 12px;
          font-size: 18px;
          font-family: PingFang SC, PingFang SC-Regular;
          font-weight: 400;
          color: #333333;
          margin-right: 24px;
          cursor: pointer;
          &:last-child {
            margin-right: 0px;
          }
        }
        .computer {
          margin-right: 24px;
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;
          width: 138px;
          height: 52px;
          background: #ffffff;
          border: 1px solid #333333;
          border-radius: 12px;
          font-size: 18px;
          font-family: PingFang SC, PingFang SC-Regular;
          font-weight: 400;
          color: #333333;
        }
      }
    }
  }
}
</style>
